<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Video as VideoModel;
use think\Db;

/**
 * 视频接口
 */
class Video extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $videoModel = null;

    public function __construct()
    {
        parent::__construct();
        $this->videoModel = new VideoModel();
    }

    /**
     * 发布视频
     */
    public function createVideo()
    {
        $userInfo = $this->auth->getUserinfo();
        //用户id
        $data['user_id'] = $userInfo['id'];
        //视频封面图
        $data['video_image'] = $this->request->post('video_image/s', '') ?: $this->error('请上传视频封面');
        //视频路径
        $data['video_url'] = $this->request->post('video_url/s', '') ?: $this->error('请上传视频路径');
        //文案描述
        $data['desc'] = $this->request->post('desc/s', '') ?: $this->error('请填写文案');
        //秒数
        $data['second'] = $this->request->post('second/d', 0);
        //每个的红包金额
        $data['money'] = $this->request->post('money');
        //填写红包数量
        $data['number'] = $this->request->post('number/d', 0);

        // if (!$userInfo['is_shoper']) $this->error('您不是商家,请先认证');

        $res = $this->videoModel->createVideo($data, $userInfo);

        return $res['code'] ? $this->success('发布成功') : $this->error($res['msg']);
    }

    /**
     * 删除视频
     */
    public function delVideo()
    {
        //视频id
        $video_id = $this->request->post('video_id/d', 0) ?: $this->error('参数错误');

        $videoInfo = $this->videoModel->where('id', $video_id)->find();

        if (!$videoInfo) return $this->error('未找到当前视频信息');

        $res = $videoInfo->delete();

        return $res ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * 我发布的视频列表
     */
    public function myVideoList()
    {
        //用户id
        $user_id = $this->auth->id;

        $limit = $this->request->post('limit/d', 10);

        $list = $this->videoModel->where(['user_id' => $user_id])->paginate($limit);

        foreach ($list as &$v) {
            $v['like_num'] = mt_rand(1, 9999);
            $v['share_num'] = mt_rand(1, 9999);
            $v['comment_num'] = mt_rand(1, 9999);
        }

        return $this->success('请求成功', $list);
    }
}
